﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Creventive.Wpf.ShellFactory.Workspace.Primitives;

namespace Creventive.Wpf.ShellFactory.Workspace
{
    /// <summary>
    /// Defines a workspace which can contain multiple views at one time.
    /// </summary>
    public class MultiViewWorkspace : MultiViewWorkspaceBase
    {
        private List<WorkspaceItem> activationStack = new List<WorkspaceItem>();

        static MultiViewWorkspace()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiViewWorkspace), new FrameworkPropertyMetadata(typeof(MultiViewWorkspace)));
        }

        /// <summary>
        ///   Called when the active workspace item has changed.
        /// </summary>
        /// <param name = "oldItem">The old item.</param>
        /// <param name = "newItem">The new item.</param>
        protected override void OnActiveWorkspaceItemChanged(WorkspaceItem oldItem, WorkspaceItem newItem)
        {
            if (oldItem == newItem)
                return;

            if (newItem != null)
            {
                this.activationStack.Remove(newItem);
                this.activationStack.Add(newItem);
            }
        }

        /// <summary>
        /// Called when a view should be activated.
        /// </summary>
        /// <param name="view">The view.</param>
        protected override void OnActivateView(ApplicationView view)
        {
            // Find the view's workspace item
            var workspaceItem = this.GetItemForView(view);

            // Set the view as the current view
            this.ActiveWorkspaceItem = workspaceItem;
        }

        /// <summary>
        /// Called when a new view has been added to the workspace.
        /// </summary>
        /// <param name="view">The view.</param>
        /// <param name="targetIndex">Where to place the new view.</param>
        protected override void OnAddView(ApplicationView view, int targetIndex)
        {
            // Create the view's workspace item
            var workspaceItem = this.CreateItem(view);

            // Set the view as the current view
            this.ActiveWorkspaceItem = workspaceItem;
        }

        /// <summary>
        /// Called when a view should be moved.
        /// </summary>
        /// <param name="view">The view.</param>
        /// <param name="sourceIndex">The original index of the view.</param>
        /// <param name="targetIndex">Where to place the view.</param>
        protected override void OnMoveView(ApplicationView view, int sourceIndex, int targetIndex)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Called when a view is to be removed from the workspace.
        /// </summary>
        /// <param name="view">The view.</param>
        protected override void OnRemoveView(ApplicationView view)
        {
            // Find the view's workspace item
            var workspaceItem = this.GetItemForView(view);

            // Remove the item from the activation stack
            this.activationStack.Remove(workspaceItem);

            // If the removed view is the active workspace item, reset it to whatever is next on the activation stack.
            if (this.ActiveWorkspaceItem == workspaceItem)
            {
                if (this.activationStack.Count > 0)
                    this.ActiveWorkspaceItem = this.activationStack.Last();
                else
                    this.ActiveWorkspaceItem = null;
            }

            // Finally destroy the workspace item.
            this.DestroyItem(workspaceItem);
        }
    }
}